<%--
  Created by IntelliJ IDEA.
  User: LiYa
  Date: 2020/9/22
  Time: 11:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.1.1.min.js"></script>
<head>
    <title></title>
</head>
<body>
<%--<form method="post" action="${pageContext.request.contextPath}/user/register">--%>
<div>
    <label>用户名</label>
    <input type="text" id="name" name="name" placeholder="请输入用户姓名">
    <label>账号</label>
    <input type="text" id="username" name="username" placeholder="请输入用户账号">
    <label>密码</label>
    <input type="password" id="password" name="password" placeholder="请输入用户密码">
    <br>
    <input type="submit" value="提交" onclick="submit1()">
</div>
<%--</form>--%>
<script>
    function submit() {
        var nameValue = $("#name").val();
        var usernameValue = $("#username").val();
        var passwordValue = $("#password").val();
        $.ajax({
            url:"${pageContext.request.contextPath}/user/ajaxregister",
            type: 'post',
            contentType: 'application/x-www-form-urlencoded',
            data: {name: nameValue,username:usernameValue, password:passwordValue},
            dataType : 'json', //期望服务器返回一个json格式的字符串
            success: function (result) { //result是一个js对象
                alert(JSON.stringify(result));
            },
            error: function (err) {
                alert(JSON.stringify(err));
            }
        });
    }

    function submit1() {
        var nameValue = $("#name").val();
        var usernameValue = $("#username").val();
        var passwordValue = $("#password").val();
        var roleValue = {id:2, name:"admin"};
        //稍微复杂的一点的js对象，有嵌套。
        var userValue = {name: nameValue,username:usernameValue, password:passwordValue, role:roleValue};
        //转成user的字符串；
        var userString = JSON.stringify(userValue);
        $.ajax({
            url:"${pageContext.request.contextPath}/user/ajaxcomplex",
            type: 'post', //只能用post
            contentType: 'application/json',
            data: userString,  //只能传字符串
            processData: false, //不要ajax内部去处理data这个参数
            dataType : 'json', //期望服务器返回一个json格式的字符串
            success: function (result) { //result是一个js对象
                alert(JSON.stringify(result));
            },
            error: function (err) {
                alert(JSON.stringify(err));
            }
        });
    }
</script>
</body>
</html>
